package cn.interestingshop.service.notice;

import cn.interestingshop.dao.NoticeDao;
import cn.interestingshop.dao.NoticeDaoImpl;
import cn.interestingshop.entity.Notice;
import cn.interestingshop.utils.DataSourceUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NoticeSserviceImpl implements NoticeService{
    @Override
    public List<Notice> selectAll(Integer currentPageNo, Integer pageSize) {

        Connection conn=null;
        List<Notice> list=new ArrayList<>();

        try {
            conn= DataSourceUtil.openConnection();
            NoticeDao noticeDao=new NoticeDaoImpl(conn);
            list=noticeDao.selectAll(currentPageNo,pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }



        return list;
    }

    @Override
    public List<Notice> selectAll() {
        Connection conn=null;
        List<Notice> list=new ArrayList<>();

        try {
            conn= DataSourceUtil.openConnection();
            NoticeDao noticeDao=new NoticeDaoImpl(conn);
            list=noticeDao.selectAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }



        return list;
    }

    @Override
    public Notice selectOne(int id) {

        Connection conn=null;
        Notice notice=new Notice();

        try {
            conn= DataSourceUtil.openConnection();
            NoticeDao noticeDao=new NoticeDaoImpl(conn);
            notice=noticeDao.selectOne(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }



        return notice;
    }

    @Override
    public int selectAllCount() {
        Connection conn=null;
        int rs=0;

        try {
            conn= DataSourceUtil.openConnection();
            NoticeDao noticeDao=new NoticeDaoImpl(conn);
            rs=noticeDao.selectAllCount();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }



        return rs;
    }
}
